package com.eastmoney.emis.edda.infrastructure.constraint.service;

import com.eastmoney.emis.edda.domain.entity.ServiceTime;
import com.eastmoney.emis.edda.domain.repository.EddaServiceTimeRepository;
import com.eastmoney.emis.edda.domain.service.EddaServeTimeService;
import com.eastmoney.emis.edda.domain.valueobject.ServiceTimeType;
import com.eastmoney.emis.edda.domain.valueobject.ServiceType;
import com.eastmoney.emis.edda.infrastructure.util.CutoffTimeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @description: Description
 * @projectName: EDDA-HSBC
 * @package: com.eastmoney.emis.edda.infrastructure.constraint.service
 * @author: 汪程程
 * @date: 2022/10/15 10:50 AM
 * @version: 1.0
 */
@Service
public class EddaServeTimeServiceImpl implements EddaServeTimeService {

    @Autowired
    private EddaServiceTimeRepository repository;

    @Autowired
    private BusinessDayService businessDayService;

    @Override
    public boolean serving(ServiceType serviceType) {
        if (serviceType == ServiceType.EDDI && CutoffTimeUtils.isEddiPauseTime()) {
            return false;
        }
        List<ServiceTime> serviceTimes = repository.findAllActive();
        return serviceTimes.stream()
                .filter(r -> r.getServiceType() == serviceType)
                .anyMatch(r -> {
                    ServiceTimeType serviceTimeType = r.getServiceTimeType();
                    if (serviceTimeType == ServiceTimeType.HK_TRADE_DAY) {
                        return serviceTimeType.test(r) && businessDayService.isBusinessDayOfToday();
                    }
                    return serviceTimeType.test(r);
                });
    }

}
